package com.grendel.tubeyourmind
{
	import com.enefekt.tubeloc.Movie;
	import com.enefekt.tubeloc.event.MovieStateChangeEvent;
	import com.grendel.tubeyourmind.core.Debug;
	import com.grendel.tubeyourmind.skins.TubeYourMindSkin;
	import com.grendel.tubeyourmind.youtube.Youtube;
	import com.grendel.tubeyourmind.youtube.events.YoutubeEvent;
	
	import mx.controls.Image;
	
	import spark.components.Group;
	import spark.components.supportClasses.SkinnableComponent;

	[SkinStates("normal", "disabled", "loading", "over", "hovered")]

	public class TubeYourMind extends SkinnableComponent
	{
		[SkinPart(required="true")]
		public var tubeLocVideo:Movie;
		
		[SkinPart(required="true")]
		public var overlayingImg:Image;
		
		[SkinPart(required="true")]
		public var videoGroup:Group;

		private var lastStateUpdate:MovieStateChangeEvent;

		private var youtube:Youtube = new Youtube();

		private var bVideoIdChanged:Boolean = false;

		private var selectedIndex:int = 0;

		private var bOverlayImgChanged:Boolean = false;

		private var _overlayImg:String;

		public function get overlayImg():String
		{
			return _overlayImg;
		}

		public function set overlayImg(value:String):void
		{
			_overlayImg = value;
			bOverlayImgChanged = true;
			invalidateProperties();
		}

		private var _videoId:String;

		public function get videoId():String
		{
			return _videoId;
		}

		public function set videoId(value:String):void
		{
			bVideoIdChanged = true;
			_videoId = value;
			invalidateProperties();
		}

		private var bVideoSizePositionChanged:Boolean = false;

		private var _videoSizePosition:Array;

		public function get videoSizePosition():Array
		{
			return _videoSizePosition;
		}

		public function set videoSizePosition(value:Array):void
		{
			bVideoSizePositionChanged = true;
			_videoSizePosition = value;
			invalidateProperties();
		}

		public function TubeYourMind()
		{
			super();
			setStyle("skinClass", TubeYourMindSkin);
			youtube.addEventListener(YoutubeEvent.PLAYLISTS_LOADED, youtubePlaylistLoadedHandler);
			youtube.addEventListener(YoutubeEvent.VIDEOS_FROM_PLAYLIST_LOADED, youtubeVideosFromPlaylistLoadedHandler);
		}

		public function loadPlaylists(userName:String):void
		{
			youtube.loadPlaylists(userName);
		}

		public function loadVideosFromPlaylist(username:String, playlist:String):void
		{
			youtube.loadVideosFromPlaylist(username, playlist);
		}

		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);

			if (instance == tubeLocVideo) 
			{
				tubeLocVideo.chromeless = true;
				tubeLocVideo.addEventListener(MovieStateChangeEvent.ON_STATE_CHANGE, onMovieStateChange);
			}		
		}

		override protected function commitProperties() : void
		{
			if (bVideoIdChanged)
			{
				bVideoIdChanged = false;
				tubeLocVideo.videoId = videoId;
				tubeLocVideo.playVideo();
			}
			
			if (bVideoSizePositionChanged && videoSizePosition.length == 4)
			{
				bVideoSizePositionChanged = false;
				videoGroup.x = videoSizePosition[0];
				videoGroup.y = videoSizePosition[1];
				videoGroup.width = videoSizePosition[2];
				videoGroup.height = videoSizePosition[3];
			}
			
			if (bOverlayImgChanged)
			{
				bOverlayImgChanged = false;
				overlayingImg.source = overlayImg;
			}
			
			super.commitProperties();
		}

		override protected function getCurrentSkinState():String
		{
			var skinState:String = "normal";
			
			if (!this.enabled)
			{
				skinState = "disabled";
			}
			else if (lastStateUpdate && (lastStateUpdate.stateCode == MovieStateChangeEvent.BUFFERING || lastStateUpdate.stateCode == MovieStateChangeEvent.PAUSED))
			{
				skinState = "loading";
			}

//			Debug.getInstance().appendDebug("lastStateUpdate.stateCode: " + (lastStateUpdate != null ? lastStateUpdate.stateCode : "null"));
			Debug.getInstance().appendDebug("skinState: " + skinState);
			return skinState;
		}

		private function youtubePlaylistLoadedHandler(event:YoutubeEvent):void
		{
//			trace(event);
		}

		private function youtubeVideosFromPlaylistLoadedHandler(event:YoutubeEvent):void
		{
			tubeLocVideo.videoId = youtube.videos[selectedIndex].id;
			tubeLocVideo.playVideo();
			Debug.getInstance().appendDebug("Youtube videos loaded from playlist");
		}

		private function onMovieStateChange(event:MovieStateChangeEvent):void
		{
			lastStateUpdate = event;
			Debug.getInstance().appendDebug(">>> " + event.stateCode);

			switch(event.stateCode)
			{
				case MovieStateChangeEvent.ENDED:
					playNextVideo();
					break;;
				case MovieStateChangeEvent.BUFFERING:
				case MovieStateChangeEvent.PAUSED:
				case MovieStateChangeEvent.PLAYING:
					Debug.getInstance().appendDebug("invalidateSkinState");
					invalidateSkinState();
					break;;
			}
		}

		public function playNextVideo():void
		{
			this.skin.currentState = "loading";
			tubeLocVideo.stopVideo();
			
			selectedIndex++;

			if (selectedIndex >= youtube.videos.length)
			{
				selectedIndex = 0;
			}

			tubeLocVideo.videoId = youtube.videos[selectedIndex].id;
			tubeLocVideo.playVideo();
			this.skin.currentState = "normal";
		}
	}
}